<template>
  <PageDialog
    :title="dialogInfo.title"
    v-model:visible="shows"
    :width="dialogInfo.width"
    :bt-list="dialogInfo.btList"
  >
    <!-- 条件栏 -->
    <NewPageFilter
      v-model:query="filterInfo.query.queryMap"
      :query-more-json="true"
      :filter-list="filterInfo.searchList"
      :filter-btn="filterInfo.searchBtn"
      :clearType="clearType"
    >
      <template v-slot:filter-stockSearchKey>
        <el-input
          v-model="filterInfo.stockSearchKey"
          placeholder="请输入产品名称\编码\规格\批号"
          @change="changeStockSearchKey"
        />
      </template>
    </NewPageFilter>
    <DialogTable
      ref="resourceTable"
      :refresh="tableInfo.refresh"
      :init-curpage="tableInfo.initCurpage"
      v-model:data="tableInfo.data"
      :api="tableInfo.api"
      v-model:query="filterInfo.query"
      :field-list="tableInfo.fieldList"
      :handle="tableInfo.handle"
      :rowKey="rowKey"
      :highlight-row="highlightRow"
      :checkedRowList="checkedRowList"
      :reserve-selection="true"
      :check-box="checkBox"
      @pageInfo="pageInfo"
      @handleEvent="handleEvent"
    >
      <template v-slot:col-storeBatchNumber="scope">
        <StoreBatchNumberTag :row="scope.row" />
      </template>
    </DialogTable>
    <!-- <div v-if="type === '1'" class="page-info">
      <span>共{{tableInfo.pageInfo.total || 0}}条</span>
      <span class="actionBtn">更多</span>
    </div> -->
  </PageDialog>
</template>
<script setup name="SelectProduct">
import { getCustomizeSetting } from '@/api/customize'
import { getBeginningDepotStockPage } from '@/api/beginning'
import StoreBatchNumberTag from './StoreBatchNumberTag'

const { proxy } = getCurrentInstance()
const emits = defineEmits()
const props = defineProps({
  highlightRow: {
    type: Boolean,
    default: false,
  },
  showDialog: {
    type: Boolean,
    default: false,
  },
  checkBox: {
    type: Boolean,
    default: true,
  },
  rowKey: {
    type: String,
    default: 'id',
  },
  api: {
    type: Function,
    default: getBeginningDepotStockPage,
  },
  showPrice: {
    type: Boolean,
  },
  query: {
    type: Object,
    default: function () {
      return {}
    },
  },
  showStock: {
    type: Boolean,
    default: false,
  },
  tableKey: {
    type: String,
    default: '',
  },
  checkedList: {
    type: Array,
    default: () => {
      return []
    },
  },
  checkedRowList: {
    type: Array,
    default: () => {
      return []
    },
  },
  saveEmpty: {
    type: Boolean,
    default: false,
  },
  type: {
    type: String,
    default: '1',
  },
})
const clearType = ref(true)
// 过滤相关配置
const filterInfo = ref({
  query: {
    page: true,
    customizeUserVO: {},
    sortString: '',
    queryMap: {
      ...props.query.queryMap,
    },
    excludeQueryList: props.query.excludeQueryList,
    queryParams: {
      ...props.query.queryParams,
    },
  },
  searchList: [],
  searchBtn: [
    { type: 'button', label: '搜索', event: search },
    { type: 'button', label: '重置', event: clearSearch },
  ],
  copyQueryMap: {},
  stockSearchKey: props.query.queryParams.stockSearchKey,
})
// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  api: undefined,
  fieldList: [],
  pageInfo: {},
})
// 弹窗相关
const dialogInfo = ref({
  title: '可选产品',
  width: '75%',
  btLoading: false,
  btList: [
    { label: '关闭', event: handleClose, show: true },
    {
      label: '保存',
      type: 'primary',
      disabled: !props.saveEmpty,
      event: save,
      show: true,
    },
  ],
})
const selectRow = ref([])
const shows = computed({
  get() {
    return props.showDialog
  },
  set(newValue) {
    emits('update:showDialog', newValue) // 更新父组件shows
  },
})

setData()
function setData() {
  let obj = {
    price: {
      label: '单价',
      value: 'price',
      hidden: true,
      type: 'column',
      show: false,
      permission: !props.showPrice,
      formatter: proxy.$fn.TripartiteMethod,
    },
    defaultSupplierPrice: {
      label: '默认供应商价格',
      value: 'defaultSupplierPrice',
      type: 'column',
      permission: !props.showPrice,
      show: false,
      formatter: proxy.$fn.TripartiteMethod,
      hidden: true,
    },
    showPrice: {
      label: '展示价格',
      value: 'showPrice',
      type: 'column',
      show: false,
      permission: !props.showPrice,
      formatter: proxy.$fn.TripartiteMethod,
      hidden: true,
    },
    commonName: {
      label: '通用名称',
      value: 'commonName',
      componentType: 'input',
      show: false,
      hidden: true,
    },
  }
  tableInfo.value.fieldList = Object.values({
    ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
    ...proxy.$fn.deepClone(obj),
  })
  filterInfo.value.searchList = Object.values({
    ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
    ...proxy.$fn.deepClone(obj),
  })
  getCustomize()
}
function changeStockSearchKey(val) {
  filterInfo.value.query.queryParams.stockSearchKey = val
  search()
}
function clearSearch() {
  clearType.value = !clearType.value
  filterInfo.value.query = proxy.$fn.deepClone(filterInfo.value.copyQueryMap)
  filterInfo.value.stockSearchKey = ''
  filterInfo.value.query.queryParams.stockSearchKey = ''
  search()
}
// 获取列表
function getList() {
  proxy.$nextTick(() => {
    tableInfo.value.refresh = Math.random()
  })
}
function search() {
  proxy.$nextTick(() => {
    tableInfo.value.initCurpage = Math.random()
    tableInfo.value.refresh = Math.random()
  })
}
function pageInfo(obj) {
  tableInfo.value.pageInfo = obj
}
function save() {
  handleClose()
  selectRow.value.forEach((item) => {
    item.accountDepotStockId = item.id
  })
  emits('callback', selectRow.value)
}
function handleEvent(event, data) {
  switch (event) {
    case 'tableClickRow':
      selectRow.value = data
      if (!props.saveEmpty) {
        dialogInfo.value.btList[1].disabled = !(data && data.id)
      }
      break
    case 'tableCheck':
      selectRow.value = data
      if (!props.saveEmpty) {
        dialogInfo.value.btList[1].disabled = !data.length
      }
      break
  }
}
// 关闭弹窗前的回调
function handleClose() {
  shows.value = false
  emits('reset')
}
function getCustomize() {
  let param = { tableKey: props.tableKey }
  getCustomizeSetting(param).then((res) => {
    if (res && res.success) {
      let data = res.data
      tableInfo.value.fieldList = proxy.$fn.setFieldList(
        tableInfo.value.fieldList,
        data.detail
      )
      tableInfo.value.fieldList.forEach((item) => {
        if (item.value === 'storeBatchNumber') {
          item['type'] = 'slot'
        }
      })
      let searchInfo = proxy.$fn.setSearchList(
        filterInfo.value.searchList,
        data.detail
      )
      console.log(searchInfo)
      filterInfo.value.searchList = searchInfo.searchList
      filterInfo.value.searchList.forEach((item) => {
        item.event = search
      })
      let stockSearchKey = {
        slot: true,
        value: 'stockSearchKey',
      }
      filterInfo.value.searchList.unshift(stockSearchKey)
      filterInfo.value.query.customizeUserVO = data
      filterInfo.value.query.queryMap = {
        ...searchInfo.queryMap,
        ...filterInfo.value.query.queryMap,
      }
      console.log(filterInfo.value.query.queryMap)
      filterInfo.value.copyQueryMap = proxy.$fn.deepClone(
        filterInfo.value.query
      )
      console.log(filterInfo.value.copyQueryMap)
      tableInfo.value.api = props.api
      getList()
    }
  })
}
</script>

<style scoped lang="scss">
.page-info {
  padding-top: 10px;
  span {
    padding-left: 10px;
  }
}
</style>
